﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace GalaxyDefenderChaos
{
    public class My2DMainMenu : My2DMenu
    {
        List<MainMenuListener> _Listeners = new List<MainMenuListener>();

        string[] _StrTextureButtons = {
                                    "Menu/NewGame",
                                    "Menu/Option",
                                    "Menu/About",
                                    "Menu/Exit"
                                };
        Song _BackgroundSound;
        string _BackgroundSoundString = "Menu/BackGroundSound";
        private SoundCenter _SoundCenter;

        public My2DMainMenu(ContentManager content, GraphicsDevice graphicsDevice, SoundCenter soundCenter)
        {
            initBaseMenu(content, graphicsDevice);
            _BackgroundSound = content.Load<Song>(_BackgroundSoundString);
            _SoundCenter = soundCenter;

            _ButtonList = new My2DButton[_StrTextureButtons.Count()];

            Vector2 backgroundSize = new Vector2(1024, 1024);
            Vector2 buttonSize = new Vector2(350, 64);
            Vector2 startPosition = new Vector2(graphicsDevice.Viewport.Width / 2 - buttonSize.X / 2,
                                                    (buttonSize.Y + 10) * _ButtonList.Count() / 2 + 30);
            Vector2 offset = new Vector2(0, buttonSize.Y + 10);

            for (int i = 0; i < _ButtonList.Count(); i++)
            {
                _ButtonList[i] = new My2DButton(content, _StrTextureButtons[i], startPosition + offset * i, buttonSize);
            }
        }

        public void PlaySound()
        {
            _SoundCenter.playLoopSound(_BackgroundSound);
        }

        public void StopSound()
        {
            _SoundCenter.pauseLoopSound();
        }

        public override void Draw(GraphicsDevice graphicsDevice, Camera camera, GameTime gameTime)
        {

            base.Draw(graphicsDevice, camera, gameTime);

            for (int i = 0; i < _ButtonList.Count(); i++)
            {
                _ButtonList[i].Draw(graphicsDevice, camera, gameTime);
            }

            graphicsDevice.BlendState = BlendState.Opaque;
        }

        public override void Update(GameTime gameTime, TouchCollection touchCollection)
        {
            if (touchCollection.Count > 0 && touchCollection[0].State == TouchLocationState.Pressed)
            {
                Vector2 touchPoint = new Vector2(touchCollection[0].Position.X, touchCollection[0].Position.Y);
                int index = GetMenuSelectedIndex(touchPoint);
                switch (index)
                { 
                    case 0:
                        StopSound();
                        NotifyNewGameSelectedEvent();
                        break;
                    case 1:
                        NotifyOptionSelectedEvent();
                        break;
                    case 2:
                        NotifyAboutSelectedEvent();
                        break;
                    case 3:
                        NotifyExitSelectedEvent();
                        break;
                }
            }
        }

        private void NotifyAboutSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].AboutSelected();
            }
        }

        private void NotifyExitSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].ExitSelected();
            }
        }

        private void NotifyOptionSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].OptionSelected();
            }
        }

        private void NotifyNewGameSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].NewGameSelected();
            }
        }

        public void AddListener(MainMenuListener listener)
        {
            _Listeners.Add(listener);
        }
    }
}
